package com.itheima.dubbo.mysql.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.pojo.domain.BlackList;
import com.itheima.dubbo.interfaces.BlackListApi;
import com.itheima.dubbo.mysql.mapper.BlackListMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/15
 * @Version 1.0
 * @describe
 */
@Service
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 分页查询用户的黑名单
     *
     * @param userId   用户id
     * @param page     当前页码
     * @param pagesize 每页多少条
     * @return
     */
    @Override
    public List<BlackList> queryBlackListByUserId(Long userId, Integer page, Integer pagesize) {
        Page<BlackList> blackListPage = blackListMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(BlackList.class).eq(BlackList::getUserId, userId).orderByDesc(BlackList::getUpdated));
        return blackListPage.getRecords();
    }

    /**
     * 删除黑名单记录
     *
     * @param userId      用户id
     * @param blackUserId 黑名单用户id
     */
    @Override
    public void removeBlackList(Long userId, Long blackUserId) {
        blackListMapper.delete(Wrappers.lambdaQuery(BlackList.class)
                .eq(BlackList::getUserId, userId)
                .eq(BlackList::getBlackUserId, blackUserId));
    }
}
